<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<%
    String path = request.getContextPath();
	String basePath = "/";
%>
<!DOCTYPE html>
<html lang="en">
<head>
    <base href="/">
    <!-- 下拉框 -->
    <link rel="stylesheet" href="static/ace/css/chosen.css"/>
    <!-- jsp文件头和头部 -->
    <%@ include file="../index/top.jsp" %>
    <!-- 日期框 -->
    <link rel="stylesheet" href="static/ace/css/datepicker.css"/>
    <style type="text/css">
        td {
            vertical-align: inherit !important;
        }
    </style>
</head>
<body class="no-skin">

<!-- /section:basics/navbar.layout -->
<div class="main-container" id="main-container">
    <div id="zhongxin" style="padding-top: 13px;"></div>
    <!-- /section:basics/sidebar -->
    <div class="main-content">
        <div class="main-content-inner">
            <div class="page-content">
                <div class="row">
                    <div class="col-xs-12">
                        <!-- 检索  -->
                        <form action="goods/goodsSpecList.do" method="post" name="goodsForm" id="goodsForm">
                            <input type="hidden" name="goodsId" value="${pd.goodsId }">
                            <table id="simple-table" class="table table-striped table-bordered table-hover">
                                <thead>
                                <tr>
                                    <th class="center">商品编号</th>
                                    <th class="center">商品名称</th>
                                    <c:if test="${label !=null}">
                                        <c:forEach items="${label}" var="t" varStatus="vs">
                                            <th class="center">${t.labelName}</th>
                                        </c:forEach>
                                    </c:if>
                                    <!-- <th class="center">操作</th> -->
                                </tr>
                                </thead>
                                <tbody id="goodlist">
                                <!-- 开始循环 -->
                                <c:choose>
                                    <c:when test="${not empty spec}">
                                        <c:if test="${QX.cha == 1 }">
                                            <c:forEach items="${spec}" var="goods" varStatus="vs">
                                                <tr>
                                                    <td class="center">${goods.code}</td>
                                                    <td class="center">${goods.goodsName}</td>
                                                    <c:if test="${goods.labelAttr !=null}">
                                                        <c:forEach items="${goods.labelAttr}" var="t" varStatus="vs">
                                                            <th class="center">${t.attrName}</th>
                                                        </c:forEach>
                                                    </c:if>
                                                        <%-- 	<td class="center">
                                                            <c:if test="${QX.edit == 1 }">
                                                                  <c:if test="${goods.state == '00' }">
                                                                  <a class="btn btn-xs btn-success" title="编辑" onclick="toGoodsSpec('${goods.id}');">
                                                                    <i class="ace-icon fa fa-pencil-square-o bigger-120" title="编辑"></i>
                                                                  </a>
                                                                  <a class="btn btn-xs" title="上架" onclick="editGoodsSpecState('${goods.id}','01','${goods.endTime}')" >
                                                                     <i class="ace-icon fa fa-lock bigger-120" title="上架"></i>
                                                                  </a>
                                                                  </c:if>
                                                                   <c:if test="${goods.state == '01' }">
                                                                  <a class="btn btn-xs btn-success" title="下架" onclick="editGoodsSpecState('${goods.id}','00','${goods.endTime}')"> <i
                                                                      class="ace-icon fa fa-unlock bigger-120"
                                                                  title="下架"></i>
                                                                </a>
                                                                </c:if>
                                                            </c:if>
                                                            </td> --%>
                                                </tr>

                                            </c:forEach>
                                        </c:if>
                                        <c:if test="${QX.cha == 0 }">
                                            <tr>
                                                <td colspan="11" class="center">您无权查看</td>
                                            </tr>
                                        </c:if>
                                    </c:when>
                                    <c:otherwise>
                                        <tr class="main_info">
                                            <td colspan="11" class="center">没有相关数据</td>
                                        </tr>
                                    </c:otherwise>
                                </c:choose>
                                </tbody>
                            </table>
                            <div class="page-header position-relative">
                                <table style="width:100%;">
                                    <tr>
                                        <%-- <c:if test="${QX.edit == 1 }">
                                          <td style="vertical-align:top;">
                                            <a class="btn btn-mini btn-success" onclick="batchUpState();">批量上架</a>
                                          </td>
                                          <td style="vertical-align:top;">
                                            <a class="btn btn-mini btn-success" onclick="batchDownState();">批量下架</a>
                                          </td>
                                          </c:if> --%>
                                        <td style="vertical-align:top;">
                                            <div class="pagination"
                                                 style="float: right;padding-top: 0px;margin-top: 0px;">${page.pageStr}</div>
                                        </td>
                                    </tr>
                                </table>
                            </div>
                        </form>

                    </div>
                    <!-- /.col -->
                </div>
                <!-- /.row -->
            </div>
            <!-- /.page-content -->
        </div>
    </div>
    <!-- /.main-content -->

    <!-- 返回顶部 -->
    <a href="#" id="btn-scroll-up" class="btn-scroll-up btn btn-sm btn-inverse">
        <i class="ace-icon fa fa-angle-double-up icon-only bigger-110"></i>
    </a>

</div>
<!-- /.main-container -->

<!-- basic scripts -->
<!-- 页面底部js¨ -->
<%@ include file="../index/foot.jsp" %>
<!-- 删除时确认窗口 -->
<script src="static/ace/js/bootbox.js"></script>
<!-- ace scripts -->
<script src="static/ace/js/ace/ace.js"></script>
<!-- 日期框 -->
<script src="static/ace/js/date-time/bootstrap-datepicker.js"></script>
<!-- 下拉框 -->
<script src="static/ace/js/chosen.jquery.js"></script>
<!--提示框-->
<script type="text/javascript" src="static/js/jquery.tips.js"></script>
</body>

<script type="text/javascript">
    $(top.hangge());

    //检索
    function searchs() {
        top.jzts();
    }


    //新增
    function add(goodId) {
        top.jzts();
        var diag = new top.Dialog();
        diag.Drag = true;
        diag.Title = "新增规格";
        diag.URL = '<%=basePath%>goods/toGoodsSpecAdd.do?goodId=${goodId}';
        diag.Width = 1800;
        diag.Height = 600;
        diag.CancelEvent = function () { //关闭事件
            location.reload();
            if (diag.innerFrame.contentWindow.document.getElementById('zhongxin').style.display == 'none') {
                if ('${page.currentPage}' == '0') {
                    top.jzts();
                    setTimeout("self.location=self.location", 100);
                } else {
                    nextPage('${page.currentPage}');
                }
            }
            diag.close();
        };
        diag.show();
    }

    function toGoodsSpec(specId) {
        top.jzts();
        var diag = new top.Dialog();
        diag.Drag = true;
        diag.Title = "修改规格";
        diag.URL = '<%=basePath%>goods/toSpecEdit.do?specId=' + specId;
        diag.Width = 1200;
        diag.Height = 700;
        diag.CancelEvent = function () { //关闭事件
            if (diag.innerFrame.contentWindow.document.getElementById('zhongxin').style.display == 'none') {
                if ('${page.currentPage}' == '0') {
                    top.jzts();
                    setTimeout("self.location=self.location", 100);
                } else {
                    nextPage('${page.currentPage}');
                }
            }
            diag.close();
        };
        diag.show();
    }

    function toDecimal(x) {
        var f = parseFloat(x);
        if (isNaN(f)) {
            return;
        }
        f = Math.round(x * 100) / 100;
        return f;
    }

    function editGoodsSpecState(id, state, endTime) {
        $.get("<%=basePath%>goods/updateSpecState.do", {
            specId: id,
            state: state,
            endTime: endTime,
        }, function (data) {
            //这里显示从服务器返回的数据
            if ("success" == data) {
                alert("修改成功");
            } else if ("failed" == data) {
                alert("修改失败");
            } else if ("timeEnd" == data) {
                alert("此商品规格下架时间已到!");
            }
            nextPage('${page.currentPage}');
        });
    }

    //复选框全选控制
    var active_class = 'active';
    $('#simple-table > thead > tr > th input[type=checkbox]').eq(0).on('click', function () {
        var th_checked = this.checked;//checkbox inside "TH" table header
        $(this).closest('table').find('tbody > tr').each(function () {
            var row = this;
            if (th_checked) $(row).addClass(active_class).find('input[type=checkbox]').eq(0).prop('checked', true);
            else $(row).removeClass(active_class).find('input[type=checkbox]').eq(0).prop('checked', false);
        });
    });

    //批量上架
    function batchUpState() {
        var str = '';
        var state = '';
        for (var i = 0; i < document.getElementsByName('ids').length; i++) {
            if (document.getElementsByName('ids')[i].checked) {
                if (document.getElementsByName('ids')[i].id != '00') {
                    alert("存在已上架规格");
                    return false;
                }
                if (str == '') {
                    str += document.getElementsByName('ids')[i].value;
                } else {
                    str += ',' + document.getElementsByName('ids')[i].value;
                }
            }
        }
        if (str == '') {
            alert("您没有选择任何规格");
            return false;
        } else {
            $.ajax({
                type: "POST",
                url: '<%=basePath%>goods/updateSpecStateList.do?tm=' + new Date().getTime(),
                data: {state: '01', ids: str},
                success: function (data) {
                    if ("success" == data) {
                        alert("批量上架成功");
                    } else if ("timeEnd" == data.substring(0, 7)) {
                        alert("id为(  " + data.substring(7, data.length) + "  )的商品规格下架时间已到，无法上架");
                    } else {
                        alert("批量上架失败");
                    }
                    nextPage('${page.currentPage}');

                }
            });
        }
    }

    //批量下架
    function batchDownState() {
        var str = '';
        var state = '';
        for (var i = 0; i < document.getElementsByName('ids').length; i++) {
            if (document.getElementsByName('ids')[i].checked) {
                if (document.getElementsByName('ids')[i].id != '01') {
                    alert("存在已下架规格");
                    return false;
                }
                if (str == '') str += document.getElementsByName('ids')[i].value;
                else str += ',' + document.getElementsByName('ids')[i].value;
            }
        }
        if (str == '') {
            alert("您没有选择任何规格");
            return false;
        } else {
            $.ajax({
                type: "POST",
                url: '<%=basePath%>goods/updateSpecStateList.do?tm=' + new Date().getTime(),
                data: {state: '00', ids: str},
                success: function (data) {
                    if ("success" == data) {
                        alert("批量下架成功");
                    } else {
                        alert("批量下架失败");
                    }
                    nextPage('${page.currentPage}');

                }
            });
        }
    }
</script>
</html>
